<?php
namespace app\common\model;

/**
 * 文件库分组模型
 */
class UploadGroup extends BaseModel
{
    // 定义表名
    protected $name = 'upload_group';

    // 定义主键
    protected $pk = 'group_id';

    /**
     * 分组详情
     */
    public static function detail($group_id) {
        return self::withoutGlobalScope()->get($group_id);
    }
    
    /**
     * 获取列表记录
     */
    public function getList(string $group_type = 'image')
    {
        $applet_id = self::$applet_id;
        $filter = [];
        $filter['group_type'] = $group_type;
        empty($applet_id) && $filter['applet_id'] = 0;
        return $this->where($filter)->order(['sort' => 'asc'])->select();
    }

    /**
     * 添加新记录
     */
    public function add(array $data)
    {
        $data['sort'] = 100;
        $data['applet_id'] = self::$applet_id;
        return $this->save($data);
    }

    /**
     * 更新记录
     */
    public function edit(array $data)
    {
        return $this->save($data) !== false;
    }

    /**
     * 删除记录
     */
    public function remove()
    {
        $applet_id = self::$applet_id;
        $filter = [];
        $filter['group_id'] = $this->group_id;
        empty($applet_id) && $filter['applet_id'] = 0;
        // 更新该分组下的所有文件
        $model = new UploadFile;
        $model->where($filter)->update(['group_id' => 0]);
        return $this->delete();
    }

}
